package com.gs.m_my_goods.persistent

import android.content.Context
import android.graphics.Bitmap
import android.os.Handler
import android.os.Looper
import com.gs.common.BaseStateCallback
import com.gs.common.callback.HttpCallback
import com.gs.common.constant.StateCode
import com.gs.m_my_goods.constant.UploadPictureErrorCode
import com.gs.m_my_goods.model.AddOrUpdateModel

class AddOrUpdatePersistent(context: Context) {

    private val addOrUpdateModel by lazy { AddOrUpdateModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun uploadPicture(picture: Bitmap, callback: OnUploadPictureStateCallback) {
        Thread {
            addOrUpdateModel.uploadPicture(picture) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onUpdatePictureLoading()
                        }
                    }

                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onUpdatePictureSuccess()
                        }
                    }

                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onUpdatePictureFailed()
                        }
                    }

                    UploadPictureErrorCode.PICTURE_EXCEED_SIZE_ERROR -> {
                        handler.post {
                            callback.onPictureExceedSizeFailed()
                        }
                    }

                    UploadPictureErrorCode.PICTURE_NULL_POINTER_ERROR -> {
                        handler.post {
                            callback.onPictureNullPointerFailed()
                        }
                    }

                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }

                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }

                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun addOrUpdateGood(
        state: String,
        username: String,
        goodId: Int? = null,
        goodTitle: String,
        goodDetailTitle: String,
        goodPicturePath: String,
        goodPrice: Double,
        goodCount: Int,
        callback: OnAddGoodStateCallback
    ) {
        Thread {
            addOrUpdateModel.addOrUpdateGood(state, username, goodId, goodTitle, goodDetailTitle, goodPicturePath, goodPrice, goodCount) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onAddGoodLoading()
                        }
                    }

                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onAddGoodSuccess()
                        }
                    }

                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onAddGoodFailed()
                        }
                    }

                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }

                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }

                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun getGoodsById(goodId: Int, callback: OnGetGoodsByIdStateCallback) {
        Thread {
            addOrUpdateModel.getGoodsById(goodId) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetGoodsByIdLoading()
                        }
                    }

                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetGoodsByIdSuccess()
                        }
                    }

                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetGoodsByIdFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }

                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }

                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    interface OnUploadPictureStateCallback : BaseStateCallback {
        fun onUpdatePictureLoading() {}
        fun onUpdatePictureSuccess()
        fun onUpdatePictureFailed()
        fun onPictureExceedSizeFailed()
        fun onPictureNullPointerFailed()
    }

    interface OnAddGoodStateCallback : BaseStateCallback {
        fun onAddGoodLoading() {}
        fun onAddGoodSuccess()
        fun onAddGoodFailed()
    }

    interface OnGetGoodsByIdStateCallback: BaseStateCallback {
        fun onGetGoodsByIdLoading() {}
        fun onGetGoodsByIdSuccess()
        fun onGetGoodsByIdFailed()
    }

    companion object {
        private var instance: AddOrUpdatePersistent? = null
        fun getInstance(context: Context): AddOrUpdatePersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = AddOrUpdatePersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}